/**
  \file TestCase_DawgUniqueNodeSet.cpp
  \author Francis Girard
  \date 22 october 2013
*/


/*
 *  Copyright (C) 2013, Francis Girard
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/config/SourcePrefix.h>

#include "dawg_unique_node_set.h"


class TestCase_DawgUniqueNodeSet : public CPPUNIT_NS::TestFixture
{
  CPPUNIT_TEST_SUITE( TestCase_DawgUniqueNodeSet );
  CPPUNIT_TEST( test_create );
  CPPUNIT_TEST( test_linearize_empty );
  CPPUNIT_TEST( test_linearize_one );
  CPPUNIT_TEST( test_linearize_two );
  CPPUNIT_TEST( test_linearize_three );
  CPPUNIT_TEST( test_linearize_many );
  CPPUNIT_TEST( test_linearize_dups );
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp();
  void tearDown();

protected:
  void test_create();
  void test_linearize_empty();
  void test_linearize_one();
  void test_linearize_two();
  void test_linearize_three();
  void test_linearize_many();
  void test_linearize_dups();
};
CPPUNIT_TEST_SUITE_REGISTRATION( TestCase_DawgUniqueNodeSet );


void TestCase_DawgUniqueNodeSet::setUp()
{
}

void TestCase_DawgUniqueNodeSet::tearDown()
{
}

void TestCase_DawgUniqueNodeSet::test_create()
{
  h_dawg_unique_node_set hNewSet = dawg_unique_node_set_create();
  CPPUNIT_ASSERT( hNewSet != 0 );
  dawg_unique_node_set_destroy(hNewSet);
}


void TestCase_DawgUniqueNodeSet::test_linearize_empty()
{
  h_dawg_unique_node_set hSet = dawg_unique_node_set_create();
  h_dawg_arr_node hArr = dawg_arr_node_create();
  h_dawg_arr_node hArr2 = dawg_arr_node_create();
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 0, (unsigned int) dawg_arr_node_size(hArr) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 0, (unsigned int) dawg_arr_node_size(hArr2) );

  dawg_unique_node_set_linearize(hSet, hArr);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 0, (unsigned int) dawg_arr_node_size(hArr) );

  dawg_unique_node_set_linearize_sorted(hSet, hArr2);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 0, (unsigned int) dawg_arr_node_size(hArr2) );

  dawg_unique_node_set_destroy(hSet);
  dawg_arr_node_destroy(hArr);
  dawg_arr_node_destroy(hArr2);
}


void TestCase_DawgUniqueNodeSet::test_linearize_one()
{
  h_dawg_unique_node_set hSet = dawg_unique_node_set_create();
  dawg_unique_node_set_add(hSet, (void*) 666);

  h_dawg_arr_node hArr = dawg_arr_node_create();
  dawg_unique_node_set_linearize(hSet, hArr);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 1, (unsigned int) dawg_arr_node_size(hArr) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 666, (unsigned int) dawg_arr_node_get_el(hArr, 0) );

  h_dawg_arr_node hArr2 = dawg_arr_node_create();
  dawg_unique_node_set_linearize_sorted(hSet, hArr2);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 1, (unsigned int) dawg_arr_node_size(hArr2) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 666, (unsigned int) dawg_arr_node_get_el(hArr2, 0) );

  dawg_unique_node_set_destroy(hSet);
  dawg_arr_node_destroy(hArr);
  dawg_arr_node_destroy(hArr2);
}


void TestCase_DawgUniqueNodeSet::test_linearize_two()
{
  h_dawg_unique_node_set hSet = dawg_unique_node_set_create();
  dawg_unique_node_set_add(hSet, (void*) 667);
  dawg_unique_node_set_add(hSet, (void*) 666);

  h_dawg_arr_node hArr = dawg_arr_node_create();
  dawg_unique_node_set_linearize(hSet, hArr);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 2, (unsigned int) dawg_arr_node_size(hArr) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 666, (unsigned int) dawg_arr_node_get_el(hArr, 0) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 667, (unsigned int) dawg_arr_node_get_el(hArr, 1) );
  dawg_arr_node_destroy(hArr);

  h_dawg_arr_node hArr2 = dawg_arr_node_create();
  dawg_unique_node_set_linearize_sorted(hSet, hArr2);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 2, (unsigned int) dawg_arr_node_size(hArr2) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 666, (unsigned int) dawg_arr_node_get_el(hArr2, 0) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 667, (unsigned int) dawg_arr_node_get_el(hArr2, 1) );
  dawg_arr_node_destroy(hArr2);

  dawg_unique_node_set_destroy(hSet);
}


void TestCase_DawgUniqueNodeSet::test_linearize_three()
{
  h_dawg_unique_node_set hSet = dawg_unique_node_set_create();
  dawg_unique_node_set_add(hSet, (void*) 667);
  dawg_unique_node_set_add(hSet, (void*) 668);
  dawg_unique_node_set_add(hSet, (void*) 666);

  h_dawg_arr_node hArr = dawg_arr_node_create();
  dawg_unique_node_set_linearize(hSet, hArr);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 3, (unsigned int) dawg_arr_node_size(hArr) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 666, (unsigned int) dawg_arr_node_get_el(hArr, 0) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 667, (unsigned int) dawg_arr_node_get_el(hArr, 1) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 668, (unsigned int) dawg_arr_node_get_el(hArr, 2) );
  dawg_arr_node_destroy(hArr);

  h_dawg_arr_node hArr2 = dawg_arr_node_create();
  dawg_unique_node_set_linearize_sorted(hSet, hArr2);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 3, (unsigned int) dawg_arr_node_size(hArr2) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 666, (unsigned int) dawg_arr_node_get_el(hArr2, 0) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 667, (unsigned int) dawg_arr_node_get_el(hArr2, 1) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 668, (unsigned int) dawg_arr_node_get_el(hArr2, 2) );
  dawg_arr_node_destroy(hArr2);

  dawg_unique_node_set_destroy(hSet);
}


void TestCase_DawgUniqueNodeSet::test_linearize_many()
{
  h_dawg_unique_node_set hSet = dawg_unique_node_set_create();
  dawg_unique_node_set_add(hSet, (void*) 667);
  dawg_unique_node_set_add(hSet, (void*) 668);
  dawg_unique_node_set_add(hSet, (void*) 666);
  dawg_unique_node_set_add(hSet, (void*) 669);
  dawg_unique_node_set_add(hSet, (void*) 665);
  dawg_unique_node_set_add(hSet, (void*) 670);
  dawg_unique_node_set_add(hSet, (void*) 664);

  h_dawg_arr_node hArr = dawg_arr_node_create();
  dawg_unique_node_set_linearize(hSet, hArr);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 7, (unsigned int) dawg_arr_node_size(hArr) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 664, (unsigned int) dawg_arr_node_get_el(hArr, 0) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 665, (unsigned int) dawg_arr_node_get_el(hArr, 1) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 666, (unsigned int) dawg_arr_node_get_el(hArr, 2) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 667, (unsigned int) dawg_arr_node_get_el(hArr, 3) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 668, (unsigned int) dawg_arr_node_get_el(hArr, 4) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 669, (unsigned int) dawg_arr_node_get_el(hArr, 5) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 670, (unsigned int) dawg_arr_node_get_el(hArr, 6) );
  dawg_arr_node_destroy(hArr);

  h_dawg_arr_node hArr2 = dawg_arr_node_create();
  dawg_unique_node_set_linearize_sorted(hSet, hArr2);
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 7, (unsigned int) dawg_arr_node_size(hArr2) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 664, (unsigned int) dawg_arr_node_get_el(hArr2, 0) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 665, (unsigned int) dawg_arr_node_get_el(hArr2, 1) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 666, (unsigned int) dawg_arr_node_get_el(hArr2, 2) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 667, (unsigned int) dawg_arr_node_get_el(hArr2, 3) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 668, (unsigned int) dawg_arr_node_get_el(hArr2, 4) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 669, (unsigned int) dawg_arr_node_get_el(hArr2, 5) );
  CPPUNIT_ASSERT_EQUAL( (unsigned int) 670, (unsigned int) dawg_arr_node_get_el(hArr2, 6) );
  dawg_arr_node_destroy(hArr2);

  dawg_unique_node_set_destroy(hSet);
}


void TestCase_DawgUniqueNodeSet::test_linearize_dups()
{
    h_dawg_unique_node_set hSet = dawg_unique_node_set_create();
    dawg_unique_node_set_add(hSet, (void*) 669);
    dawg_unique_node_set_add(hSet, (void*) 667);
    dawg_unique_node_set_add(hSet, (void*) 667);
    dawg_unique_node_set_add(hSet, (void*) 668);
    dawg_unique_node_set_add(hSet, (void*) 666);
    dawg_unique_node_set_add(hSet, (void*) 669);
    dawg_unique_node_set_add(hSet, (void*) 664);
    dawg_unique_node_set_add(hSet, (void*) 665);
    dawg_unique_node_set_add(hSet, (void*) 664);
    dawg_unique_node_set_add(hSet, (void*) 670);
    dawg_unique_node_set_add(hSet, (void*) 664);

    h_dawg_arr_node hArr = dawg_arr_node_create();
    dawg_unique_node_set_linearize(hSet, hArr);

    CPPUNIT_ASSERT_EQUAL( (unsigned int) 7, (unsigned int) dawg_arr_node_size(hArr) );


    CPPUNIT_ASSERT_EQUAL( (unsigned int) 664, (unsigned int) dawg_arr_node_get_el(hArr, 0) );
    CPPUNIT_ASSERT_EQUAL( (unsigned int) 665, (unsigned int) dawg_arr_node_get_el(hArr, 1) );
    CPPUNIT_ASSERT_EQUAL( (unsigned int) 666, (unsigned int) dawg_arr_node_get_el(hArr, 2) );
    CPPUNIT_ASSERT_EQUAL( (unsigned int) 667, (unsigned int) dawg_arr_node_get_el(hArr, 3) );
    CPPUNIT_ASSERT_EQUAL( (unsigned int) 668, (unsigned int) dawg_arr_node_get_el(hArr, 4) );
    CPPUNIT_ASSERT_EQUAL( (unsigned int) 669, (unsigned int) dawg_arr_node_get_el(hArr, 5) );
    CPPUNIT_ASSERT_EQUAL( (unsigned int) 670, (unsigned int) dawg_arr_node_get_el(hArr, 6) );

    dawg_unique_node_set_destroy(hSet);
    dawg_arr_node_destroy(hArr);
}
